package com.xujf.sym

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Handler
import android.os.Process
import android.widget.Toast
import com.xujf.sym.util.HandlerUtils
import java.util.ArrayList

/**
 * Created by XuJiafeng on 2018/1/26.
 * Content：启动页 + 权限校验
 */
class PermissionActivity : Activity(), Runnable {
    var PERMISSION_REQ = 0x123456

    private val mPermission = arrayOf(
            Manifest.permission.INTERNET,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.ACCESS_WIFI_STATE)

    private val mRequestPermission = ArrayList<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_transition)

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            for (one in mPermission) {
                if (PackageManager.PERMISSION_GRANTED != this.checkPermission(one, Process.myPid(), Process.myUid())) {
                    mRequestPermission.add(one)
                }
            }
            if (!mRequestPermission.isEmpty()) {
                this.requestPermissions(mRequestPermission.toTypedArray(), PERMISSION_REQ)
                return
            }
        }
        startActiviy()
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        // 版本兼容
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
            return
        }
        if (requestCode == PERMISSION_REQ) {
            for (i in grantResults.indices) {
                for (one in mPermission) {
                    if (permissions[i] == one && grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                        mRequestPermission.remove(one)
                    }
                }
            }
            startActiviy()
        }
    }

    public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        if (requestCode == PERMISSION_REQ) {
            if (resultCode == 0) {
                this.finish()
            }
        }
    }

    private fun startActiviy() {
        if (mRequestPermission.isEmpty()) {
//            val mProgressDialog = ProgressDialog(this)
//            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER)
//            mProgressDialog.setTitle("loading register data...")
//            mProgressDialog.setCancelable(false)
//            mProgressDialog.show()
            Thread(Runnable {
//                val app = this@PermissionActivity.applicationContext as SSFaceApplication
//                app.mFaceDB.loadFaces()
                this@PermissionActivity.runOnUiThread(java.lang.Runnable {
//                    mProgressDialog.cancel()
                    HandlerUtils.handler.postDelayed(this, 1000)
                })
            }).start()
        } else {
            Toast.makeText(this, "应用相关权限被禁用，请前往设置-应用管理，开启权限", Toast.LENGTH_LONG).show()
            Handler().postDelayed({ this@PermissionActivity.finish() }, 3000)
        }
    }

    override fun run() {
        startActivity(Intent(this, MainTabActivity::class.java))
        finish()
    }
}
